package com.cloudsigns.factorymanager2.project.model;


import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetProduceOrderListModel implements IGetProduceOrderListModel {

    @Override
    public void getProductOrderList(final OnProductOrderListListener productOrderListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpClient client = new DefaultHttpClient();
                HttpPost httpGet = new HttpPost(ProjectUtil.GET_PRODUCT_ORDER_LIST);

                HttpResponse response;
                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);

                    Log.e("GetProductOrderList","result="+result);
                    Log.e("GetProductOrderList","json="+json.toString());

                    String code = json.getString("code");
                    if (null != productOrderListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("productOrderResponseList"));
                            List<ProduceProduceOrderBean> produceOrderList = new ArrayList<ProduceProduceOrderBean>();
                            if (jsonArr != null) {

                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        ProduceProduceOrderBean produceOrderBean = new ProduceProduceOrderBean();
                                        produceOrderBean.setOrderId(jsonObj.getString("orderId"));
                                        String isHurry = jsonObj.getString("isHurry");
                                        if (null != isHurry) {
                                            if (isHurry.equals("HURRY")) {
                                                produceOrderBean.setIsHurry(CommonEnum.ProduceOrderIsHurry.HURRY);
                                            } else if (isHurry.equals("NOT_HURRY")) {
                                                produceOrderBean.setIsHurry(CommonEnum.ProduceOrderIsHurry.NOT_HURRY);
                                            }
                                        }

                                        String level = jsonObj.getString("level");
                                        if (null != level) {
                                            if (level.equals("VERY_IMPORTANT")) {
                                                produceOrderBean.setLevel(CommonEnum.ProduceOrderLevel.VERY_IMPORTANT);
                                            } else if (level.equals("MORE_IMPORTANT")) {
                                                produceOrderBean.setLevel(CommonEnum.ProduceOrderLevel.MORE_IMPORTANT);
                                            } else if (level.equals("GENERALLY_IMPORTANT")) {
                                                produceOrderBean.setLevel(CommonEnum.ProduceOrderLevel.GENERALLY_IMPORTANT);
                                            } else if (level.equals("NORMAL")) {
                                                produceOrderBean.setLevel(CommonEnum.ProduceOrderLevel.NORMAL);
                                            }
                                        }

                                        produceOrderBean.setEngineeringName(jsonObj.getString("engineeringName"));
                                        produceOrderBean.setProjectName(jsonObj.getString("projectName"));
                                        produceOrderBean.setType(jsonObj.getString("type"));

                                        String status = jsonObj.getString("status");
                                        if (null != status) {
                                            if (status.equals("CREATED")) {
                                                produceOrderBean.setStatus(CommonEnum.ProduceOrderStatus.CREATED);
                                            }  else if (status.equals("SEND")) {
                                                produceOrderBean.setStatus(CommonEnum.ProduceOrderStatus.SEND);
                                            } else if (status.equals("RECEIVED")) {
                                                produceOrderBean.setStatus(CommonEnum.ProduceOrderStatus.RECEIVED);
                                            } else if (status.equals("RETURNED")) {
                                                produceOrderBean.setStatus(CommonEnum.ProduceOrderStatus.RETURNED);
                                            }  else if (status.equals("COMPLETED")) {
                                                produceOrderBean.setStatus(CommonEnum.ProduceOrderStatus.COMPLETED);
                                            }
                                        }
                                        produceOrderList.add(produceOrderBean);
                                    }
                                }
                                productOrderListListener.getDataSuccessed(produceOrderList);
                            }
                        } else {
                            productOrderListListener.getDataFailed(code + "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnProductOrderListListener {
        void getDataSuccessed(List<ProduceProduceOrderBean> getProductOrderList);

        void getDataFailed(String desc);
    }
}
